package com.fuxinglianbao.services.impl;

import com.fuxinglianbao.models.PaymentOrder;
import com.fuxinglianbao.models.WithdrawRecord;
import com.fuxinglianbao.services.PaymentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.UUID;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Override
    @Transactional
    public PaymentOrder createPaymentOrder(Long userId, BigDecimal amount, PaymentOrder.PaymentType paymentType,
                                         String notifyUrl, String returnUrl) {
        PaymentOrder order = new PaymentOrder();
        order.setOrderNo(generateOrderNo());
        order.setUserId(userId);
        order.setAmount(amount);
        order.setPaymentType(paymentType);
        order.setStatus(PaymentOrder.PaymentStatus.PENDING);
        order.setCreateTime(LocalDateTime.now());
        order.setNotifyUrl(notifyUrl);
        order.setReturnUrl(returnUrl);

        // 根据支付类型调用不同的支付接口
        switch (paymentType) {
            case WECHAT:
                // 调用微信支付接口
                handleWechatPay(order);
                break;
            case ALIPAY:
                // 调用支付宝支付接口
                handleAlipayPay(order);
                break;
            case BANK_CARD:
                // 调用银行卡支付接口
                handleBankCardPay(order);
                break;
        }

        return order;
    }

    @Override
    @Transactional
    public void handlePaymentNotify(String orderNo, String transactionId, PaymentOrder.PaymentStatus status) {
        PaymentOrder order = getPaymentOrder(orderNo);
        if (order != null) {
            order.setTransactionId(transactionId);
            order.setStatus(status);
            order.setPayTime(LocalDateTime.now());
            // 更新订单状态
            // 处理业务逻辑，如更新用户余额等
        }
    }

    @Override
    public PaymentOrder getPaymentOrder(String orderNo) {
        // 实现查询订单逻辑
        return null;
    }

    @Override
    @Transactional
    public WithdrawRecord createWithdrawRequest(Long userId, BigDecimal amount, WithdrawRecord.WithdrawType withdrawType,
                                              String accountInfo) {
        WithdrawRecord record = new WithdrawRecord();
        record.setWithdrawNo(generateWithdrawNo());
        record.setUserId(userId);
        record.setAmount(amount);
        record.setWithdrawType(withdrawType);
        record.setStatus(WithdrawRecord.WithdrawStatus.PENDING);
        record.setCreateTime(LocalDateTime.now());

        // 根据提现类型设置账户信息
        switch (withdrawType) {
            case WECHAT:
                record.setWechatOpenId(accountInfo);
                break;
            case ALIPAY:
                record.setAlipayAccount(accountInfo);
                break;
            case BANK_CARD:
                // 假设accountInfo格式为: 银行名称|卡号|开户名
                String[] bankInfo = accountInfo.split("\\|");
                record.setBankName(bankInfo[0]);
                record.setBankCardNo(bankInfo[1]);
                record.setAccountName(bankInfo[2]);
                break;
        }

        return record;
    }

    @Override
    @Transactional
    public void processWithdrawRequest(String withdrawNo, WithdrawRecord.WithdrawStatus status, String remark) {
        WithdrawRecord record = getWithdrawRecord(withdrawNo);
        if (record != null) {
            record.setStatus(status);
            record.setProcessTime(LocalDateTime.now());
            record.setRemark(remark);

            if (status == WithdrawRecord.WithdrawStatus.APPROVED) {
                // 调用对应的提现接口
                switch (record.getWithdrawType()) {
                    case WECHAT:
                        handleWechatWithdraw(record);
                        break;
                    case ALIPAY:
                        handleAlipayWithdraw(record);
                        break;
                    case BANK_CARD:
                        handleBankCardWithdraw(record);
                        break;
                }
            }
        }
    }

    @Override
    public WithdrawRecord getWithdrawRecord(String withdrawNo) {
        // 实现查询提现记录逻辑
        return null;
    }

    private String generateOrderNo() {
        return "PAY" + System.currentTimeMillis() + UUID.randomUUID().toString().substring(0, 8);
    }

    private String generateWithdrawNo() {
        return "WD" + System.currentTimeMillis() + UUID.randomUUID().toString().substring(0, 8);
    }

    private void handleWechatPay(PaymentOrder order) {
        // 实现微信支付逻辑
    }

    private void handleAlipayPay(PaymentOrder order) {
        // 实现支付宝支付逻辑
    }

    private void handleBankCardPay(PaymentOrder order) {
        // 实现银行卡支付逻辑
    }

    private void handleWechatWithdraw(WithdrawRecord record) {
        // 实现微信提现逻辑
    }

    private void handleAlipayWithdraw(WithdrawRecord record) {
        // 实现支付宝提现逻辑
    }

    private void handleBankCardWithdraw(WithdrawRecord record) {
        // 实现银行卡提现逻辑
    }
}